package com.bamboo.tool.view.component.dialog;

import com.bamboo.tool.entity.QueryClassParameter;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.*;

/**
 * 导出SmartDoc html弹出框
 */
public class ExportSmartDocHtmlDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private TextFieldWithBrowseButton outputDirectory;
    private FileChooserDescriptor myFileDescriptor;
    /**
     * 选中参数
     */
    private QueryClassParameter queryClassParam;
    private Project project;

    public ExportSmartDocHtmlDialog(QueryClassParameter queryClassParam, @Nullable Project project) {

        this.project = project;
        this.myFileDescriptor = FileChooserDescriptorFactory.createSingleFileDescriptor();
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        this.queryClassParam = queryClassParam;
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // 点击 X 时调用 onCancel()
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
        outputDirectory.addBrowseFolderListener("测试1", "测试2", project, myFileDescriptor);
        // 遇到 ESCAPE 时调用 onCancel()
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        String path="C:\\Users\\EDZ\\Desktop\\2017-08-3.json";
        ApplicationManager.getApplication().runReadAction(() -> {
//            Collection<BambooClass> apiClass = BambooToolComponent.getStoreService().getApiClass(queryClassParam);
//            List<ApiInfo> apiInfos = BambooToApiDoc.classToApiInfo(apiClass);
//            String content = JSONObject.toJSONString(apiInfos);
//
//            boolean exist =FileUtil.exist(path);
//            if (exist) {
//                FileUtil.del(path);
//            }
//            FileUtil.writeUtf8String(content, path);

        });


        // 在此处添加您的代码
        dispose();
    }

    private void onCancel() {
        // 必要时在此处添加您的代码
        dispose();
    }


    private void createUIComponents() {
        // TODO: place custom component creation code here

    }

}
